
package com.ps.helpdesk.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ps.helpdesk.business.BusinessGrupo;
import com.ps.helpdesk.business.BusinessUsuario;
import com.ps.helpdesk.model.Grupo;
import com.ps.helpdesk.model.Usuario;
import com.ps.helpdesk.persistence.ManagerConnection;

/**
 * Servlet implementation class for Servlet: FormGrupo
 * 
 */
public class FormGrupo extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	static final long serialVersionUID = 1L;

	public FormGrupo() {

		super();
		
		ManagerConnection.DATA_BASE = "helpdesk";
	}

	protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		doPost( request, response );
	}

	protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		if ( request.getParameter( "edit" ) != null 
				&& String.valueOf( request.getParameter( "edit" ) ).trim().length() > 0 ) {
			find( request, response );
		}
		else if ( request.getParameter( "delete" ) != null 
				&& String.valueOf( request.getParameter( "delete" ) ).trim().length() > 0 ) {
			delete( request, response );
		}
		else if ( "inserir".equals( request.getParameter( "submit" ) ) 
				|| "alterar".equals( request.getParameter( "submit" ) ) ) {
			persist( request, response );
		}
		else if ( "novo".equals( request.getParameter( "submit" ) ) ) {
			send( request, response, "?idgrupo=&descricao=&idusuarioadm=" );
		}
		else {
			send( request, response, "" );
		}
	}
	
	private void loadSituacoes( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
		
		BusinessGrupo bg = new BusinessGrupo();
		List<Grupo> grupos = bg.findAll();

		if ( request.getAttribute( "lista" ) == null ) {
			request.setAttribute( "lista", grupos );
		}
		else {
			((List<Grupo>)request.getAttribute( "lista" )).addAll( grupos );
		}		

		BusinessUsuario bsusr = new BusinessUsuario();
		List<Usuario> usuarios = bsusr.findAll();
		
		if ( request.getAttribute( "usuarios" ) == null ) {
			request.setAttribute( "usuarios", usuarios );
		}
		else {
			((List<Usuario>)request.getAttribute( "usuarios" )).addAll( usuarios );
		}
	}
	
	private void send( HttpServletRequest request, HttpServletResponse response, String parameters ) throws ServletException, IOException {
		
		loadSituacoes( request, response );
		getServletContext().getRequestDispatcher( "/pages/admin/grupo.jsp" + parameters ).forward( request, response );
	}
	
	private void sendError( HttpServletRequest request, HttpServletResponse response, BusinessGrupo bs ) throws ServletException, IOException {
		
		if ( bs != null ) {
			request.setAttribute( "mensagem", bs.getMensagem() );
		}
	
		getServletContext().getRequestDispatcher( 
				"/pages/erropage.jsp?link=/helpdesk/FormGrupo" )
					.forward( request, response );
	}

	private void find( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessGrupo bg = new BusinessGrupo();
		Grupo grupo = bg.find( request.getParameter( "edit" ) );

		if ( grupo != null ) {
			send( request, response, 
					"?idgrupo=" + grupo.getIdgrupo() + 
					"&descricao=" + grupo.getDescricao() + 
					"&idusuarioadm=" + grupo.getUsuario().getIdusuario() );
		}
		else {
			sendError( request, response, bg );
		}
	}

	private void persist( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessGrupo bg = new BusinessGrupo();
		
		Grupo grupo = new Grupo();
		grupo.setIdgrupo( Integer.parseInt( request.getParameter( "idgrupo" ).trim() ) );
		grupo.setDescricao( request.getParameter( "descricao" ).trim() );
		
		BusinessUsuario burs = new BusinessUsuario();
		
		grupo.setUsuario( burs.find( request.getParameter( "idusuarioadm" ).trim() ) );
		
		if ( bg.persist( grupo ) ) {
			send( request, response, 
					"?idgrupo=" + grupo.getIdgrupo() + 
					"&descricao=" + grupo.getDescricao() + 
					"&idusuarioadm=" + grupo.getUsuario().getIdusuario() );
		}
		else {
			sendError( request, response, bg );
		}
	}

	private void delete( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessGrupo bg = new BusinessGrupo();		
		Integer key = Integer.parseInt( request.getParameter( "delete" ).trim() );
		
		if ( bg.remove( key ) ) {
			send( request, response, "?idgrupo=&descricao=&idusuarioadm=" );
		}
		else {
			sendError( request, response, bg );
		}
	}
}